<template>
  <div class="user-avatar" v-if="userInfo">
    <el-dropdown @command="handleCommand">
      <div class="avatar-container">
        <el-avatar 
          :size="36" 
          :src="avatarUrl" 
          class="user-avatar-img"
        >
          <img src="https://cube.elemecdn.com/3/7c/3ea6beec64369c2642b92c6726f1epng.png" />
        </el-avatar>
        <span class="username">{{ userInfo.username }}</span>
        <el-icon class="dropdown-icon">
          <arrow-down />
        </el-icon>
      </div>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item command="profile">
            <el-icon><user /></el-icon>
            个人中心
          </el-dropdown-item>
          <el-dropdown-item command="logout" divided>
            <el-icon><switch-button /></el-icon>
            退出登录
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>


<script setup>
import { computed, onMounted, ref } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown, User, SwitchButton } from '@element-plus/icons-vue'

const router = useRouter()
const userInfo = ref(null)

// 计算头像URL
const avatarUrl = computed(() => {
  if (userInfo.value?.avatar) {
    return userInfo.value.avatar.startsWith('http') 
      ? userInfo.value.avatar 
      : `/api${userInfo.value.avatar}`
  }
  return ''
})

const loadUserInfo = () => {
  const userInfoStr = localStorage.getItem('userInfo')
  if (userInfoStr) {
    userInfo.value = JSON.parse(userInfoStr)
  }
}

const handleCommand = (command) => {
  switch (command) {
    case 'profile':
      router.push('/user/profile')
      break
    case 'logout':
      localStorage.removeItem('token')
      localStorage.removeItem('userInfo')
      ElMessage.success('退出登录成功')
      router.push('/login')
      break
  }
}

onMounted(() => {
  loadUserInfo()
})
</script>

<style scoped>
.user-avatar {
  display: flex;
  align-items: center;
  margin-left: auto;
}

.avatar-container {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 6px 12px;
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.9);
  backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.2);
  transition: all 0.3s ease;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.avatar-container:hover {
  background: rgba(255, 255, 255, 1);
  transform: translateY(-1px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.user-avatar-img {
  border: 2px solid #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.username {
  color: #333;
  font-size: 14px;
  font-weight: 500;
  margin: 0 4px;
}

.dropdown-icon {
  color: #666;
  font-size: 12px;
  transition: transform 0.2s ease;
}

.avatar-container:hover .dropdown-icon {
  transform: rotate(180deg);
}

:deep(.el-dropdown-menu__item) {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 16px;
}

:deep(.el-dropdown-menu__item .el-icon) {
  margin-right: 0;
}
</style> 